<?php
	require('coach.doc.php');
	//验证手机号
	$app->get(
		'/coach/mobile/check',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['mobile'])){
				$echo['status'] = false;
				$coach = $db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile']));
				if($data['mobile']!=1 && $data['type']==0){
					$member = $db2->select(1, 'tb_member', '*', array('mobile' => $data['mobile']));
				}
				if($member || $coach){
					$echo['status'] = true;
				}
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
	//注册
	$app->post(
		'/coach/register',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['password']) && isset($data['realname']) && isset($data['idcard']) && isset($data['sex']) && isset($_FILES['photo']) && isset($data['captcha'])){
				if(!$db2->select(1, 'tb_member', '*', array('mobile' => $data['mobile'])) && !$db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile']))){
					if(!$db2->select(1, 'tb_coach', '*', array('idcard' => $data['idcard'])) && !$db2->select(1, 'tb_member', '*', array('idcard' => $data['idcard']))){
						if(checkCaptcha($data['mobile'], $data['captcha'], 21)){
							$set = array(
								'mobile' => $data['mobile'],
								'password' => $data['password'],
								'nickname' => $data['realname'],
								'realname' => $data['realname'],
								'idcard' => $data['idcard'],
								'sex' => $data['sex'],
								'dt' => date('Y-m-d H:i:s')
							);
							$echo['coachid'] = $db2->insert('tb_coach', $set);
							$photopath = 'dofiles/coach/'.$echo['coachid'].'/photo/';
							create($photopath);
							$photourl = $photopath.date('YmdHis').'.jpg';
							copy($_FILES['photo']['tmp_name'], $photourl);
							$db2->update('tb_coach', array('photo' => $photourl), array('tbid' => $echo['coachid']));
						}else{
							$echo['error'] = "验证码错误";
						}
					}else{
						$echo['error'] = "身份证号已存在";
					}
				}else{
					$echo['error'] = "手机号已存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//登录
	$app->post(
		'/coach/login',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile'])){
				$coach = $db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile']));
				if($coach){
					if(isset($data['password']) && $coach['password'] == $data['password']){
						if($coach['state'] == 1){
							$echo['coachid'] = $coach['tbid'];
						}else{
							if($coach['state']==0){
								$coachstate="审核失败";
							}elseif($coach['state']==2){
								$coachstate="审核中";
							}else{
								$coachstate="您已被冻结，请联系管理员";
							}
							$echo['error'] = $coachstate;
						}
					}else{
						$echo['error'] = "登录密码错误";
					}
				}else{
					if($db2->select(2, 'tb_member', '*', array('mobile' => $data['mobile']))!=0){
						$echo['error'] = "手机号已注册会员版";
					}else{
						$echo['error'] = "手机号不存在";
					}
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//重新申请
	$app->post(
		'/coach/reapply',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['password']) && isset($data['realname']) && isset($data['idcard']) && isset($data['sex']) && isset($_FILES['photo'])){
				$coach = $db2->select(1, 'tb_coach', '*', array('mobile' => $data['mobile'], 'password' => $data['password']));
				if($coach){
					if($coach['password'] == $data['password']){
						if(!$db2->select(1, 'tb_coach', '*', array('idcard' => $data['idcard'],'mobile[!]'=>$data['mobile'])) && !$db2->select(1, 'tb_member', '*', array('idcard' => $data['idcard']))){
							$photopath = 'dofiles/coach/'.$coach['tbid'].'/photo/';
							create($photopath);
							$photourl = $photopath.date('YmdHis').'.jpg';
							move_uploaded_file($_FILES['photo']['tmp_name'], $photourl);
							$headpath = 'dofiles/coach/'.$coach['coachid'].'/head/';
							create($headpath);
							$headurl = $headpath.'head.jpg';
							move_uploaded_file($_FILES['photo']['tmp_name'], $headurl);
							$set = array(
								'realname' => $data['realname'],
								'sex' => $data['sex'],
								'idcard' => $data['idcard'],
								'photo' => $photourl,
								'state' => 2
							);
							$db2->update('tb_coach', $set, array('tbid' => $coach['tbid']));
						}else{
							$echo['error'] = "身份证号已存在";
						}
					}else{
						$echo['error'] = "登录密码错误";
					}
				}else{
					$echo['error'] = "手机号不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//找回密码
	$app->post(
		'/coach/password/find',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['password']) && isset($data['captcha'])){
				if(checkCaptcha($data['mobile'], $data['captcha'], 22)){
					$db2->update('tb_coach', array('password' => $data['password']), array('mobile' => $data['mobile']));
				}else{
					$echo['error'] = "验证码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改密码
	$app->post(
		'/coach/password/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['oldpassword']) && isset($data['newpassword'])){
				$m = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				if($m['password'] == $data['oldpassword']){
					$db2->update('tb_coach', array('password' => $data['newpassword']), array('tbid' => $data['coachid']));
					XingeApp::PushAccountAndroid(XG_ANDROID_ACCESS_ID, XG_ANDROID_SECRET_KEY, '密码修改通知', '你的登录密码已修改', 'gym'.strval($data['coachid']));
				}else{
					$echo['error'] = "原密码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改交易密码
	$app->post(
		'/coach/paypassword/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['oldpaypassword']) && isset($data['newpaypassword'])){
				$m = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				if($m['paypassword'] == $data['oldpaypassword']){
					$db2->update('tb_coach', array('paypassword' => $data['newpaypassword']), array('tbid' => $data['coachid']));
				}else{
					$echo['error'] = "原支付密码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改交易密码（通过短信验证码）
	$app->post(
		'/coach/paypassword/updatebycaptcha',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['paypassword']) && isset($data['captcha'])){
				$m = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				if(checkCaptcha($m['mobile'], $data['captcha'], 23)){
					$db2->update('tb_coach', array('paypassword' => $data['paypassword']), array('tbid' => $data['coachid']));
				}else{
					$echo['error'] = "验证码错误";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//验证交易密码
	$app->post(
		'/coach/paypassword/check',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['paypassword'])){
				$echo['status'] = false;
				if($db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid'], 'paypassword' => $data['paypassword']))){
					$echo['status'] = true;
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//查询用户信息
	$app->get(
		'/coach/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['coachid'])){
				$coach = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				if($coach){
					$echo['coachid'] = $coach['tbid'];
					$echo['head'] = file_exists('dofiles/coach/'.$coach['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$coach['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
					$echo['mobile'] = $coach['mobile'];
					$echo['nickname'] = $coach['nickname'];
					$echo['balance'] = $coach['balance'];
					$echo['issetpaypassword'] = $coach['paypassword'] ? true : false;
					$echo['level'] = $coach['level'];
					$echo['adaa'] = $coach['adaa'];
					$echo['sign'] = $db2->select(2, 'tb_sign', '*', array(
						'userid' => $coach['tbid'],
						'usertype' => 2,
						'type' => 1
					));
					$echo['realname'] = $coach['realname'];
					$echo['idcard'] = $coach['idcard'];
					$echo['photo'] = SITEROOTURL.$coach['photo'];
					$echo['sex'] = $coach['sex'];
					
					$echo['back_img'] = SITEROOTURL.$coach['back_img'];
					
					//star
					$sql = "select avg(star) as star from tb_evaluate where reserve_id in".
					" ( select tbid from tb_reserve where curriculum_id in (select tbid from tb_curriculum_snap where coach_id=?) )";
					$ht = $db2->prepare($sql);
					$ht->execute(array($data['coachid']));
					$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
					if($tmp){
					   $echo['star'] = round($tmp[0]['star'],1);
					}else{
					    $echo['star'] = 0;
					}
					
					//好评数
					$sql = "select count(*) as sum from tb_evaluate where reserve_id in".
					    " ( select tbid from tb_reserve where curriculum_id in (select tbid from tb_curriculum_snap where coach_id=?) ) and star >3";
					$ht = $db2->prepare($sql);
					$ht->execute(array($data['coachid']));
					$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
					if($tmp){
					    $echo['sum'] = round($tmp[0]['sum'],1);
					}else{
					    $echo['sum'] = 0;
					}
					
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改昵称
	$app->post(
		'/coach/nickname/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['nickname'])){
				$db2->update('tb_coach', array('nickname' => $data['nickname']), array('tbid' => $data['coachid']));
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改头像
	$app->post(
		'/coach/head/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($_FILES['head'])){
				$headpath = 'dofiles/coach/'.$data['coachid'].'/head/';
				create($headpath);
				$headurl = $headpath.'head.jpg';
				move_uploaded_file($_FILES['head']['tmp_name'], $headurl);
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//查询用户资料
	$app->get(
		'/coach/intro',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['coachid'])){
				$coach = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				if($coach){
					$echo['intro'] = $coach['intro'];
					$echo['title'] = $coach['title'];
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//修改资料
	$app->post(
		'/coach/intro/update',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['intro']) && isset($data['title'])){
			    $set = array();
			    if($_FILES['back_img']){
    			    $backpath = 'dofiles/coach/'.$data['coachid'].'/back/';
    			    create($backpath);
    			    $backurl = $backpath.date('YmdHis').'.jpg';
    			    move_uploaded_file($_FILES['back_img']['tmp_name'], $backurl);
    			    
    			    $set['back_img'] = $backurl;
			    }
			    
			    $set['intro'] = urldecode($data['intro']);
			    $set['title'] = $data['title'];
				$db2->update('tb_coach', $set, array('tbid' => $data['coachid']));
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
